<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="userId">
        <el-select v-model="queryParams.userId" filterable placeholder="请选择用户，可输入搜索" @change="sameChange">
          <el-option
            v-for="item in sameList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
      <el-table-column label="用户姓名" align="center" prop="userId">
        <template slot-scope="scope">
          {{ scope.row.easyUser.nickName }}
        </template>
      </el-table-column>
      <el-table-column label="课程名称" align="center" prop="courseId">
        <template slot-scope="scope">
          {{ scope.row.easyCourseCard.name }}
        </template>
      </el-table-column>
      <el-table-column label="教练姓名" align="center" prop="coachId">
        <template slot-scope="scope">
          {{ scope.row.easyCoach.name }}
        </template>
      </el-table-column>
      <el-table-column label="剩余多少次" align="center" prop="surplus"/>
      <el-table-column label="已使用多少次" align="center" prop="used"/>
      <el-table-column label="创建时间" align="center" prop="createAt" width="180"/>
      <el-table-column label="更新时间" align="center" prop="updateAt" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:card:edit']"
          >更换教练
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 更换教练弹出层 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="教练" prop="coachId">
          <el-select v-model="form.coachId" filterable @change="coachChange" placeholder="请选择教练">
            <el-option
              v-for="item in coachList"
              :key="item.id"
              :label="item.name"
              :value="item.id" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程" prop="courseId">
          <el-select v-model="form.courseId" filterable  placeholder="请选择课程">
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTimeCard, listCard ,updateCoach} from '@/api/debao/timeUserCourse'
import { samelist } from '@/api/system/user'
import { alllistCoach } from '@/api/debao/coach'

export default {
  name: 'Card',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程卡次卡表格数据
      cardList: [],
      // 弹出层标题
      title: '',
      /**
       * 普通用户
       */
      sameList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        courseId: null,
        coachId: null,
        surplus: null,
        used: null,
        createAt: null,
        updateAt: null
      },
      // 表单参数
      form: {},
      coachList: [],
      courseList: [],
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' }
        ],
        courseId: [
          { required: true, message: '课程ID不能为空', trigger: 'blur' }
        ],
        coachId: [
          { required: true, message: '教练ID不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getSamelist()
    this.getCoachList()
  },
  methods: {
    /** 查询课程卡次卡列表 */
    getList() {
      this.loading = true
      listCard(this.queryParams).then(response => {
        this.cardList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    //获取所有教练列表
    getCoachList() {
      alllistCoach().then(res => {
        this.coachList = res.data
      })
    },
    coachChange(e) {
      getTimeCard(e, this.form.typeId).then(res => {
        this.courseList = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /**
     * 获取普通用户列表
     */
    getSamelist() {
      samelist().then(response => {
        this.sameList = response.data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseId: null,
        coachId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.id = row.id
      this.form.typeId = row.easyCourseCard.typeId
      this.open = true
      this.title = '教练调整'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateCoach(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
